package com.dha.dao.impl;

import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import javax.jws.WebMethod;
import javax.jws.WebParam;
import javax.jws.WebService;

import org.hibernate.Query;
import org.hibernate.SQLQuery;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;

import com.dha.custom.GiaoVienCustom;
import com.dha.custom.ThongBaoCustom;
import com.dha.custom.XemThoiKhoaBieuCustom;
import com.dha.dao.GiaoVienDAO;
import com.dha.utility.Utility;

@WebService(targetNamespace = "GiaoVienService")
public class GiaoVienDAOImpl implements GiaoVienDAO {
	private SessionFactory sessionFactory = new Configuration().configure()
			.buildSessionFactory();
	private Session session;

	@WebMethod(operationName = "layDanhSachGiaoVien")
	@Override
	public List<GiaoVienCustom> danhSachGiaoVien() {
		// TODO Auto-generated method stub
		return Utility.layDanhSachGiaoVien();
	}
	@WebMethod(operationName = "LayDanhSachGiaoVienTheoNgay")
	@Override
	public ArrayList<GiaoVienCustom> danhSachGiaoVienTheoNgay(@WebParam(name="ngay") String ngay) {
		try {
			session = sessionFactory.openSession();
			session.beginTransaction();
			
			String sql = "select distinct tb.maGiaoVien, gv.tenGiaoVien "
					+ "from ThongBao tb, GiaoVien gv "
					+ "where tb.maGiaoVien = gv.maGiaoVien "
					+ "and LEFT(CONVERT(varchar,tb.ngayDang,120),10) = ?";
			
			SQLQuery query = (SQLQuery) session.createSQLQuery(sql)
					.setParameter(0, ngay);
			
			List<Object> ketQua = query.list();
			ArrayList<GiaoVienCustom> arrayList = new ArrayList<GiaoVienCustom>();

			Iterator iterator = ketQua.iterator();
			
			while (iterator.hasNext()) {
				Object[] dong = (Object[]) iterator.next();
				String maGiaoVien = (String) dong[0];
				String tenGiaoVien = (String) dong[1];
				
				GiaoVienCustom gv = new GiaoVienCustom(maGiaoVien, tenGiaoVien);
				arrayList.add(gv);

			}
			
			return arrayList;
		} catch (Exception e) {
			// TODO: handle exception
			session.getTransaction().rollback();
			return null;
		}
		finally{
			session.close();
		}
	}

}
